/**
 * Copyright (C) 2010 Sergey Grigoriev <s.a.grigoriev@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.gmf.compile

import org.apache.maven.project.MavenProject
import com.google.inject.Inject
import net.gmf.dsl.FlexProject
import net.gmf.core.OSCommand
import net.gmf.core.FlexSdkFacade

/**
 * Re-usable chunks of code
 */
final class CompilationHelper {

  @Inject
  private MavenProject project


  @Inject
  private FlexProject gmfConfig


  @Inject
  private FlexSdkFacade flexSdkFacade

  /**
   * Adds default options to  the command
   * @param cmd mxmlc or compc command
   */
  void addDefaultOptions(OSCommand cmd) {
    if (new File(localesFolder).exists() && new File(localesFolder).isDirectory()) {
      cmd << ["-compiler.source-path=": [localesFolder, '{locale}'].join(File.separator)]
    }
    if (new File(project.build.sourceDirectory).exists()) {
      cmd << ["-compiler.source-path=": project.build.sourceDirectory]
    }
    cmd << gmfConfig.rootNode.compiler.ops
  }

  /**
   * Copy FlexSDK frameworks into the specified folder
   * @param into destination folder
   */
  void copyFrameworkRsls(String into) {
    if (gmfConfig.rootNode.copyFrameworkRsls) {
      flexSdkFacade.copyFrameworkRsls(into)
    }
  }

  /**
   * Locales folder
   * @return path to folder as string
   */
  String getLocalesFolder() { [project.basedir, 'src', 'locales', 'flex'].join(File.separator) }

  /**
   * Detect if source folders exist
   * @return true if source folder exists, otherwise return false
   */
  boolean sourceFilesExist() {
    new File(project.build.sourceDirectory).exists() || new File(localesFolder).exists()
  }
}
